var MARQUEE_TIMER = null;

//文字跑马灯
function starMarquee(el, flag) {
  clearInterval(MARQUEE_TIMER);
  var step = 0;
  var view = el.find('.item-view');
  var text = el.find('.item-text');
  if (view.length == 0 && text.length) {
    return false;
  }
  var textW = text.width();
  var viewW = view.width();
  if (textW > viewW) {
    MARQUEE_TIMER = setInterval(function () {
      step += 1;
      if (step >= textW) {
        step = -viewW;
      }
      text.css({
        left: -step + 'px'
      })
    }, 30);
  } else {
    text.css('position', 'relative')
  }
}

//停止跑马灯
function stopMarquee(el) {
  clearInterval(MARQUEE_TIMER);
  var text = el.find('.item-text');
  if (text.length === 0) {
    return false;
  }
  text.css('left', 0);
}

// 鉴权
function playFilmData(obj) {
  var domain = getDomain().domain;
  var _data = null;
  $.ajax({
    type: "get",
    async: false,
    url: domain + "/datajsp/au_PlayFilmData.jsp?mediaCode=" + obj.mediaCode,
    success: function (data) {
      //返回结果
      _data = eval("(" + data + ")");
    },
    error: function () {
      //错误，直接跳转：根目录下 infoDisplay.html?errod_code=&returnUrl=
    }
  })
  return _data;
}

// 订购
function order(obj) {
  var domain = getDomain().domain;
  window.location.href = domain + "/datajsp/orderServer.jsp?ProductID=" + obj.ProductID + '&ContentID=' + obj.mediaCode + '&ReturnURL=' + obj.ReturnURL + '&backUrl=' + obj.backUrl;
}

// function ordered(obj){
//   var domain = getDomain().domain;
//   window.location.href = domain + "/datajsp/orderServer.jsp?ProductID=" + obj.ProductID + '&ContentID=27b13558e38349168a72855abe6cfb85&ReturnURL=' + obj.ReturnURL + '&backUrl=' + obj.backUrl;
// }


// 获取视频播放
function getVideos() {
  var typeid = TYPEIDS.VIDEOS
  var total = getVodListByTypeId({
    TYPEID: typeid,
    LENGTH: 1,
    STATION: 1
  }).total
  var station = Math.floor(Math.random() * total);
  data = getVodListByTypeId({
    TYPEID: typeid,
    LENGTH: 1,
    STATION: station
  }).data;
  return data;
}


// 获取根目录
function getDomain() {
  var data = getDomainPath();
  var urls = {};
  if (data.code == 200) {
    var page = data.data.page;
    if (page.indexOf('?') > 0) {
      var _domain = page.substring(0, page.indexOf('?'));
    } else {
      _domain = page;
    }
    urls.home = page.replace(/陶/g, '&');
    urls.domain = _domain.substring(0, _domain.lastIndexOf('/')) + '/'
  }
  return urls
}

function addDoMain(url) {
  url = unescape(url);
  var return_index = url.indexOf('ReturnURL=');
  if (return_index > 0) {
    returnUrl = url.substring(return_index + 10);
    addDomainPath(returnUrl.replace(/&/g, '陶'));
    first = true;
  } else {
    // addDomainPath(HOST)
  }
}

// 通过节目的状态跳转页面
/* 
  params: sitcom

*/
function programGoUrl(sitcom, form) {
  var ctype = '动画';

  // var filmDate = playFilmData({
  //   mediaCode: sitcom.code
  // })
  // var isSub = filmDate.forwardJson.isSub;

  // if (sitcom.charge == 1) {
  //   if (isSub == 1 || isSub == 5 || isSub == 98) {
  //     if (sitcom.ctype == ctype) {
  //       GY.addUrl(PANDA_PATH + 'series.html?VODID=' + sitcom.ID, form);
  //     } else {
  //       GY.addUrl(PANDA_PATH + 'order-list.html?mediaCode=' + sitcom.code + '&returnUrl=' + escape(form));
  //     }
  //     return;
  //   }
  // }

  if (sitcom.ctype == ctype) {
    GY.addUrl(PANDA_PATH + 'series.html?VODID=' + sitcom.ID, form);
    return;
  } else {
    GY.addUrl(PANDA_PATH + 'vod-play.html?VODID=' + sitcom.ID, form);
    return;
  }

}



// 调试代码
function showTest(param) {
  var str = '';
  if (typeof param == 'object') {
    str = ObjToStr(param)
  } else {
    str = param
  }
  var old = $('#test').html()
  $('#test').html(str + '<br/>' + old);
}

// 将对象转化为字符串
function ObjToStr(obj) {
  var str = '';
  for (var attr in obj) {
    str += '&' + attr + '=' + obj[attr];
  }
  return str.substring(1);
}

// 鉴权
function getFilmDate() {
  filmDate = playFilmData({
    mediaCode: '27b13558e38349168a72855abe6cfb85'
  })

  isSub = filmDate.forwardJson.isSub;
  visible = isSub == 0 ? false : true;
}